<?php

namespace Home\Controller;

use Think\Controller;

class StudentController extends BaseControlle
{
    public function __construct()
    {
        parent::__construct();
        ini_set('date.timezone','Asia/Shanghai');
        $this->assign('time',time());
        $uid = I('session.studentid',0,'intval');
        $student_exam_result = M('student_exam_result');
        $exam_count = $student_exam_result->where(array('student_id'=>$uid))->count();
        $this->assign('exam_count',$exam_count);
    }

    public function index()
    {
        Header("HTTP/1.1 301 Moved Permanently");
        Header("Location: ".U('Home/Student/Open'));
    }

    public function Open()
    {
        $exam_assignment = M('exam_assignment');
        $page = I('get.page', 0, 'intval');
        $count = $exam_assignment->count();
        $page = DBPage($count, 30, $page);

        $exam_assignment_ret = $exam_assignment
            ->limit(30)
            ->page($page['page'])
            ->order('id desc')
            ->where(array('unit' => 3))
            ->select();
        $this->assign('ExamType', 3);
        $this->assign('ExamName', '公开试卷');
        $this->assign('nav', 'Student_User');
        $this->assign('ret', $exam_assignment_ret);
        $this->assign('count', $page['countPage']);
        $this->assign('page', $page['page']);
        $this->display('index');
    }

    public function GradeExam()
    {
        $exam_assignment = M('exam_assignment');
        $student_information = M('student_information');

        $uid = I('session.studentid',0,'intval');
        $page = I('get.page', 0, 'intval');
        $count = $exam_assignment->count();
        $page = DBPage($count, 30, $page);
        $DB_PREFIX = C('DB_PREFIX');

        $ret_student_information = $student_information->where(array('uid'=>$uid))->find();

        $exam_assignment_ret = $exam_assignment
            ->join("__EXAM_GRADE__ ON __EXAM_GRADE__.assignmentid=__EXAM_ASSIGNMENT__.id")
            ->limit(30)
            ->page($page['page'])
            ->order("{$DB_PREFIX}exam_assignment.id desc")
            ->where(array('unit' => 2,'grade'=>$ret_student_information['grade']))
            ->field("{$DB_PREFIX}exam_assignment.*")
            ->select();
        $this->assign('ExamType', 2);
        $this->assign('ExamName', '年级试卷');
        $this->assign('nav', 'Student_User');
        $this->assign('ret', $exam_assignment_ret);
        $this->assign('count', $page['countPage']);
        $this->assign('page', $page['page']);
        $this->display('index');
    }

    public function MyCenterExam()
    {
        $uid = I('session.studentid',0,'intval');
        $exam_assignment = M('exam_assignment');
        $page = I('get.page', 0, 'intval');
        $count = $exam_assignment->count();
        $page = DBPage($count, 30, $page);
        $DB_PREFIX = C('DB_PREFIX');

        $exam_assignment_ret = $exam_assignment
            ->join("__EXAM_STUDENT__ ON __EXAM_STUDENT__.assignmentid=__EXAM_ASSIGNMENT__.id")
            ->limit(30)
            ->page($page['page'])
            ->order("{$DB_PREFIX}exam_assignment.id desc")
            ->where(array("{$DB_PREFIX}exam_assignment.unit" => 1,"{$DB_PREFIX}exam_student.uid"=>$uid))
            ->field("{$DB_PREFIX}exam_assignment.*")
            ->select();
        $this->assign('ExamType', 1);
        $this->assign('ExamName', '个人试卷');
        $this->assign('nav', 'Student_User');
        $this->assign('ret', $exam_assignment_ret);
        $this->assign('count', $page['countPage']);
        $this->assign('page', $page['page']);
        $this->display('index');
    }


    public function ExamResult()
    {
        $uid = I('session.studentid',0,'intval');
        $DB_PREFIX = C('DB_PREFIX');

        $student_exam_result = M('student_exam_result');
        $exam_result = $student_exam_result
            ->join("__EXAM_ASSIGNMENT__ ON __EXAM_ASSIGNMENT__.id=__STUDENT_EXAM_RESULT__.exam_assignment_id")
            ->where(array('student_id'=>$uid,'finish_time'=>array('neq',0)))
            ->field("{$DB_PREFIX}student_exam_result.*,{$DB_PREFIX}exam_assignment.examtitle")
            ->order("{$DB_PREFIX}student_exam_result.id DESC")
            ->select();

        $this->assign('nav', 'Student_User');
        $this->assign('ret', $exam_result);
        $this->assign('ExamType', 5);
        $this->assign('ExamName', '考试结果');
        $this->display('index');

    }
}